import sys

def get_python_obj_size(obj, seen=None):
    """Recursively finds size of objects"""
    size = sys.getsizeof(obj)
    if seen is None:
        seen = set()
    obj_id = id(obj)
    if obj_id in seen:
        return 0
    # Mark as seen
    seen.add(obj_id)
    if isinstance(obj, dict):
        size += sum([get_python_obj_size(v, seen) for v in obj.values()])
        size += sum([get_python_obj_size(k, seen) for k in obj.keys()])
    elif hasattr(obj, '__dict__'):
        size += get_python_obj_size(obj.__dict__, seen)
    elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)):
        size += sum([get_python_obj_size(i, seen) for i in obj])
    return size

if __name__ == "__main__":
    # 字典
    a = {'a': 1, 'b': 2, 'c': [3, 4], 'd': {'e': 5, 'f': 6}, 'g': (7, 8)}
    # 计算字典的总内存大小
    total_size = get_python_obj_size(a)
    print(f"dict size:{total_size} bytes")

    # 列表
    b = [1, 2, 3, 4, 5, 6, 8, "111", "222", "333"]
    total_size = get_python_obj_size(b)
    print(f"list size:{total_size} bytes")